/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cl.alanis.gui;

import cl.alanis.business.ProductoBusiness;
import cl.alanis.business.VentaBusiness;
import cl.alanis.dto.ProductoDto;
import cl.alanis.dto.StockDto;
import cl.alanis.dto.VentaDto;
import cl.alanis.exception.VentaExisteException;
import cl.alanis.util.AlanisLog;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.apache.log4j.Logger;

/**
 *
 * @author Pía
 */
public class VentaGui extends javax.swing.JDialog {
    Double sumaIva=new Double(0);
    Double sumaTotalIva=new Double(0);
    Double sumaTotalSinIva=new Double(0);
    Double sumaTotalGeneral=new Double(0);
    Logger logger=AlanisLog.obtenerLogger(VentaGui.class);
    /**
     * Creates new form VentaGui
     */
    public VentaGui() {
        this.setModal(true);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtFiltro = new javax.swing.JTextField();
        bttIngresarCodigoVenta = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblListaProductoVenta = new javax.swing.JTable();
        lblErrorVenta = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        cmbFiltroBusqueda = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        lblValorDescuento = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        lblTotalConIva = new javax.swing.JLabel();
        lblIva = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        lblTotalProductoVenta = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        lblTotalSinIva = new javax.swing.JLabel();
        lblTotalGeneral = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        cmbDescuento = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        bttLimpiarVenta = new javax.swing.JButton();
        bttCancelarVenta = new javax.swing.JButton();
        byyAceptarVenta = new javax.swing.JButton();
        btnQuitar = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblStock = new javax.swing.JTable();
        txtNumBoleta = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N
        setForeground(new java.awt.Color(255, 51, 102));

        bttIngresarCodigoVenta.setText("Buscar");
        bttIngresarCodigoVenta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bttIngresarCodigoVentaActionPerformed(evt);
            }
        });

        tblListaProductoVenta.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código Stock", "Codigo Producto", "Código Proveedor", "Descripción", "Aplicación", "Marca Auto", "Año Auto", "País Origen", "Fabricante", "Tipo Repuesto", "Valor S/IVA", "Iva", "Valor Total"
            }
        ));
        tblListaProductoVenta.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblListaProductoVentaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblListaProductoVenta);

        lblErrorVenta.setForeground(new java.awt.Color(255, 51, 51));
        lblErrorVenta.setText("-");

        jLabel10.setText("- NUEVA VENTA -");

        cmbFiltroBusqueda.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todos", "Código Producto", "Código Proveedor" }));

        jLabel4.setText("Resultados:");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED, null, new java.awt.Color(255, 0, 102), null, null), "Información de la Venta", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Consolas", 1, 12), new java.awt.Color(0, 0, 204))); // NOI18N

        jLabel2.setText("Total General");

        jLabel5.setText("Total Sin IVA");

        jLabel7.setText("$");

        lblValorDescuento.setText("-");

        jLabel20.setText("$");

        jLabel19.setText("Valor Descuento");

        lblTotalConIva.setText("-");

        lblIva.setText("-");

        jLabel16.setText("Total con IVA");

        jLabel14.setText("$");

        jLabel15.setText("$");

        lblTotalProductoVenta.setText("-");

        jLabel13.setText("IVA");

        lblTotalSinIva.setText("-");

        lblTotalGeneral.setText("-");

        jLabel11.setText("Total de Productos:");

        jLabel8.setText("$");

        cmbDescuento.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0%", "1%", "2%", "3%", "4%", "5%", "6%", "7%", "8%", "9%", "10%", "11%", "12%", "13%", "14%", "15%", "16%", "17%", "18%", "19%", "20%", "21%", "22%", "23%", "24%", "25%", "26%", "27%", "28%", "29%", "30%" }));
        cmbDescuento.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbDescuentoItemStateChanged(evt);
            }
        });
        cmbDescuento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbDescuentoActionPerformed(evt);
            }
        });

        jLabel3.setText("Descuento");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel19)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(47, 47, 47)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addGap(18, 18, 18)
                                .addComponent(lblTotalGeneral))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel20)
                                .addGap(18, 18, 18)
                                .addComponent(lblValorDescuento))))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel13)
                                .addComponent(jLabel16))
                            .addGap(47, 47, 47)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel14)
                                    .addGap(18, 18, 18)
                                    .addComponent(lblTotalConIva))
                                .addComponent(cmbDescuento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGap(1, 1, 1)
                                    .addComponent(jLabel11)
                                    .addGap(18, 18, 18)
                                    .addComponent(lblTotalProductoVenta))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel5)
                                    .addGap(50, 50, 50)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel15)
                                        .addComponent(jLabel7))))
                            .addGap(18, 18, 18)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(lblTotalSinIva)
                                .addComponent(lblIva))))
                    .addComponent(jLabel3))
                .addContainerGap(74, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(lblTotalProductoVenta))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblTotalSinIva)
                        .addComponent(jLabel7)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel15)
                        .addComponent(lblIva))
                    .addComponent(jLabel13, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(jLabel14)
                    .addComponent(lblTotalConIva))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cmbDescuento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel20)
                        .addComponent(lblValorDescuento))
                    .addComponent(jLabel19))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel8)
                        .addComponent(lblTotalGeneral))
                    .addComponent(jLabel2))
                .addContainerGap())
        );

        bttLimpiarVenta.setText("Limpiar");
        bttLimpiarVenta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bttLimpiarVentaActionPerformed(evt);
            }
        });

        bttCancelarVenta.setText("Cancelar");
        bttCancelarVenta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bttCancelarVentaActionPerformed(evt);
            }
        });

        byyAceptarVenta.setText("Aceptar");
        byyAceptarVenta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                byyAceptarVentaActionPerformed(evt);
            }
        });

        btnQuitar.setText("Quitar");
        btnQuitar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitarActionPerformed(evt);
            }
        });

        tblStock.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Cod Stock", "Cod Producto", "Descripcion", "Valor Sin Iva", "Iva", "Total"
            }
        ));
        jScrollPane3.setViewportView(tblStock);

        jLabel1.setText("Numero de boleta");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cmbFiltroBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(27, 27, 27)
                                .addComponent(txtFiltro, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(bttIngresarCodigoVenta))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 443, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(btnQuitar))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(55, 55, 55)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(byyAceptarVenta, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel1))
                                        .addGap(26, 26, 26)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(bttLimpiarVenta, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(27, 27, 27)
                                                .addComponent(bttCancelarVenta))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(txtNumBoleta, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(28, 28, 28)
                                                .addComponent(lblErrorVenta, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE)))))))
                        .addContainerGap(96, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 967, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(480, Short.MAX_VALUE)
                .addComponent(jLabel10)
                .addGap(471, 471, 471))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFiltro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bttIngresarCodigoVenta)
                    .addComponent(cmbFiltroBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnQuitar)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNumBoleta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(lblErrorVenta, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(48, 48, 48)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(byyAceptarVenta)
                            .addComponent(bttLimpiarVenta)
                            .addComponent(bttCancelarVenta))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmbDescuentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbDescuentoActionPerformed
        
    }//GEN-LAST:event_cmbDescuentoActionPerformed

    private void byyAceptarVentaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_byyAceptarVentaActionPerformed
        
        VentaBusiness business = new VentaBusiness();
        lblErrorVenta.setText("----");
        DefaultTableModel model = (DefaultTableModel) tblStock.getModel();
        
        if(tblStock.getRowCount()>0){
            if(!"".equals(txtNumBoleta.getText())){
                String temp = (String) cmbDescuento.getSelectedItem();
                String porcentajeStr = temp.substring(0, temp.length() - 1);
                Float porcentaje = Float.valueOf(porcentajeStr) / 100;
                StockDto[] stocks=new StockDto[model.getRowCount()];
                VentaDto venta=new VentaDto();       
                venta.setDescuento(porcentaje);
                venta.setFechaVenta((new Date()).getTime());
                venta.setValorTotal(Math.ceil(Double.valueOf(lblTotalGeneral.getText())));
                venta.setNumBoleta(txtNumBoleta.getText());
                for(int i=0;i<model.getRowCount();i++){
                    stocks[i]=new StockDto();
                    stocks[i].setCodStock(model.getValueAt(i, 0).toString());
                }
                try {
                    business.generaVenta(venta,stocks);
                    this.dispose();
                }catch(VentaExisteException ex) {
                    logger.error("Error:"+ex.getMessage());
                    lblErrorVenta.setText(ex.getMessage());
                }catch (Exception ex) {
                    logger.error("Error:"+ex.getMessage());
                    lblErrorVenta.setText(ex.getMessage());
                }
                
            }
            else
                JOptionPane.showMessageDialog(null, "Debe Ingresar un numero de boleta", "Venta", JOptionPane.PLAIN_MESSAGE);
            
        }
        else{
            JOptionPane.showMessageDialog(null, "Debe seleccionar al menos un producto", "Venta", JOptionPane.PLAIN_MESSAGE);
        }
        
    }//GEN-LAST:event_byyAceptarVentaActionPerformed

    private void bttIngresarCodigoVentaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bttIngresarCodigoVentaActionPerformed
        lblErrorVenta.setText("----");
        DefaultTableModel model = (DefaultTableModel) tblListaProductoVenta.getModel();
        VentaBusiness businessVenta = new VentaBusiness();
        ProductoBusiness businessProducto=new ProductoBusiness();
        String filtro=txtFiltro.getText();
        ProductoDto producto=new ProductoDto();
       while (model.getRowCount()>0){
            model.removeRow(0);
        }
       switch(cmbFiltroBusqueda.getSelectedIndex()){
           case 0:producto.setFiltroGeneral(filtro); break;
           case 1: producto.setCodProducto(filtro); break;
           case 2: producto.setCodProveedor(filtro);break;
           
               
       }
        try {
            List listaProducto=businessProducto.buscarProductoFiltro3(producto);
            Iterator iterador=listaProducto.iterator();
            while(iterador.hasNext()) {
                producto=(ProductoDto) iterador.next();
                Object[] fila=businessProducto.obtenerFilaTablaBuscaProd3(producto);
                model.addRow(fila);
            }
            
        } catch (Exception ex) {
            logger.error("Error:"+ex.getMessage());
            lblErrorVenta.setText(ex.getMessage());
        }
    }//GEN-LAST:event_bttIngresarCodigoVentaActionPerformed

    private void bttCancelarVentaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bttCancelarVentaActionPerformed
        this.dispose();
    }//GEN-LAST:event_bttCancelarVentaActionPerformed

    private void cmbDescuentoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbDescuentoItemStateChanged

        if (!"".equals(lblTotalConIva.getText())){
            Double total = Double.valueOf(lblTotalConIva.getText());
            String temp = (String) cmbDescuento.getSelectedItem();
            String porcentajeStr = temp.substring(0, temp.length() - 1);
            Double porcentaje = Double.valueOf(porcentajeStr) / 100;
            lblValorDescuento.setText(String.valueOf(Math.ceil(total * porcentaje)));
            lblTotalGeneral.setText(String.valueOf(Math.ceil(total * (1 - porcentaje))));
        }
        
    }//GEN-LAST:event_cmbDescuentoItemStateChanged

    private void bttLimpiarVentaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bttLimpiarVentaActionPerformed
        limpiarFormulario();
    }//GEN-LAST:event_bttLimpiarVentaActionPerformed

    private void tblListaProductoVentaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblListaProductoVentaMouseClicked
        DefaultTableModel modelTablaProducto = (DefaultTableModel) tblListaProductoVenta.getModel();
        DefaultTableModel modelTablaStock = (DefaultTableModel) tblStock.getModel();
        boolean sw=false;
        int fila=tblListaProductoVenta.getSelectedRow();
        Integer codigo=(Integer) tblListaProductoVenta.getValueAt(fila, 0);
        String codProd=(String) tblListaProductoVenta.getValueAt(fila, 1);
        String descripcion=(String) tblListaProductoVenta.getValueAt(fila, 3);
        Integer valor=(Integer) tblListaProductoVenta.getValueAt(fila, 10);
        Double iva=(Double) tblListaProductoVenta.getValueAt(fila, 11);
        Double total=(Double) tblListaProductoVenta.getValueAt(fila, 12);
        for(int i=0;i<modelTablaStock.getRowCount();i++){
                Integer temp=(Integer) modelTablaStock.getValueAt(i, 0);
                if(codigo.equals(temp)){
                    sw=true;
                }
                    
         }
        if(!sw){
            Object[] row=new Object[]{codigo,codProd,descripcion,valor,iva,total};
            modelTablaStock.addRow(row);
            lblTotalProductoVenta.setText(String.valueOf(tblStock.getRowCount()));
            sumaTotalSinIva+=valor;
            sumaIva+=iva;
            sumaTotalIva+=total;
            lblTotalSinIva.setText(String.valueOf(sumaTotalSinIva));
            lblIva.setText(String.valueOf(sumaIva));
            lblTotalConIva.setText(String.valueOf(sumaTotalIva));
            lblTotalGeneral.setText(String.valueOf(sumaTotalIva));
            cmbDescuento.setSelectedIndex(0);
        }
        else
            JOptionPane.showMessageDialog(null, "Codigo de stock ya ingresado", "Venta", JOptionPane.PLAIN_MESSAGE);
        
    }//GEN-LAST:event_tblListaProductoVentaMouseClicked

    private void btnQuitarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitarActionPerformed
        DefaultTableModel model = (DefaultTableModel) tblStock.getModel();
        int fila=tblStock.getSelectedRow();
        Integer valor=0;
        Double iva=0d;
        Double total=0d;
        if(fila!=-1){
            valor=(Integer) tblStock.getValueAt(fila, 3);
            iva=(Double) tblStock.getValueAt(fila, 4);
            total=(Double) tblStock.getValueAt(fila, 5);
            sumaTotalSinIva-=valor;
            sumaIva-=iva;
            sumaTotalIva-=total;
            lblTotalSinIva.setText(String.valueOf(sumaTotalSinIva));
            lblIva.setText(String.valueOf(sumaIva));
            lblTotalConIva.setText(String.valueOf(sumaTotalIva));
            lblTotalGeneral.setText(String.valueOf(sumaTotalIva));
            cmbDescuento.setSelectedIndex(0);
            model.removeRow(fila);
            lblTotalProductoVenta.setText(String.valueOf(tblStock.getRowCount()));
        }
        else
            JOptionPane.showMessageDialog(null, "Debe seleccionar un registro", "Venta", JOptionPane.PLAIN_MESSAGE);
    }//GEN-LAST:event_btnQuitarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VentaGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VentaGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VentaGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VentaGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new VentaGui().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnQuitar;
    private javax.swing.JButton bttCancelarVenta;
    private javax.swing.JButton bttIngresarCodigoVenta;
    private javax.swing.JButton bttLimpiarVenta;
    private javax.swing.JButton byyAceptarVenta;
    private javax.swing.JComboBox cmbDescuento;
    private javax.swing.JComboBox cmbFiltroBusqueda;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblErrorVenta;
    private javax.swing.JLabel lblIva;
    private javax.swing.JLabel lblTotalConIva;
    private javax.swing.JLabel lblTotalGeneral;
    private javax.swing.JLabel lblTotalProductoVenta;
    private javax.swing.JLabel lblTotalSinIva;
    private javax.swing.JLabel lblValorDescuento;
    private javax.swing.JTable tblListaProductoVenta;
    private javax.swing.JTable tblStock;
    private javax.swing.JTextField txtFiltro;
    private javax.swing.JTextField txtNumBoleta;
    // End of variables declaration//GEN-END:variables

    private void limpiarFormulario() {
        txtFiltro.setText("");
        DefaultTableModel model = (DefaultTableModel) tblListaProductoVenta.getModel();
        DefaultTableModel productosSeleccionados=(DefaultTableModel) tblStock.getModel();
        while (model.getRowCount()>0){
            model.removeRow(0);
        }
        while (productosSeleccionados.getRowCount()>0){
            productosSeleccionados.removeRow(0);
        }
        lblTotalProductoVenta.setText("");
        lblTotalSinIva.setText("");
        lblIva.setText("");
        lblTotalConIva.setText("");
        cmbDescuento.setSelectedIndex(0);
        lblValorDescuento.setText("");
        lblTotalGeneral.setText("");
        lblErrorVenta.setText("");
        cmbDescuento.setSelectedIndex(0);
        cmbFiltroBusqueda.setSelectedIndex(0);
        txtNumBoleta.setText("");
    }
}
